import Toast from "@vant/weapp/toast/toast";

Page({
  /**
   * 登录
   */
  onLogin1() {
    //  1. 获取用户加密数据
    wx.getUserProfile({
      desc: "desc",
      success: (res) => {
        console.log("=== getUserProfile", res);
        // 取出 encryptedData 与 iv
        const { encryptedData, iv } = res;

        //  2. 获取 code
        wx.login({
          success: (res) => {
            console.log("=== login code", res.code);
            // 取出 code
            const { code } = res;

            // 3. 调用后端小程序登录接口
            wx.request({
              method: "POST",
              url: "http://127.0.0.1:8001/app/user/login/mini",
              data: {
                code,
                encryptedData,
                iv,
              },
              success: ({ data }) => {
                if (data.code !== 1000) {
                  wx.showToast({ title: data.message, icon: "none" });
                  return;
                }

                // wx.showToast({ title: "登录成功" });
                Toast({
                  message: "登录成功",
                  onClose: () => {
                    // 4. 存储 token
                    wx.setStorageSync("token", data.data.token);

                    // 5. 获取数据通道对象
                    const eventChannel = this.getOpenerEventChannel();
                    // 6. 触发 loginSuccess 事件
                    eventChannel.emit("loginSuccess");
                    // 7. 跳转 页面
                    wx.navigateBack();
                  },
                });
              },
            });
          },
        });
      },
      fail: (error) => {
        console.log("=== getUserProfile fail", error);
      },
    });
  },

  pmLogin() {
    return new Promise((resolve) => {
      wx.login({
        success: (res) => {
          // 将 res.code 给 resolve 出去
          resolve(res.code);
        },
      });
    });
  },

  async onLogin2() {
    //  1. 获取用户的加密数据
    const { encryptedData, iv } = await wx.getUserProfile({
      desc: "desc",
    });

    // 2. 获取 code
    const code = await this.pmLogin();
    console.log("=== code", code);

    // 3. 调用登录接口
    wx.request({
      method: "POST",
      url: "http://127.0.0.1:8001/app/user/login/mini",
      data: {
        code,
        encryptedData,
        iv,
      },
      success: ({ data }) => {
        if (data.code !== 1000) {
          wx.showToast({ title: data.message, icon: "none" });
          return;
        }

        // wx.showToast({ title: "登录成功" });
        Toast({
          message: "登录成功",
          onClose: () => {
            // 4. 存储 token
            wx.setStorageSync("token", data.data.token);

            // 5. 获取数据通道对象
            const eventChannel = this.getOpenerEventChannel();
            // 6. 触发 loginSuccess 事件
            eventChannel.emit("loginSuccess");
            // 7. 跳转 页面
            wx.navigateBack();
          },
        });
      },
    });
  },

  async onPhone(event) {
    console.log("=== event", event);
    // event.detail 有 encryptedData iv

    // 1. 获取手机号码加密数据
    const { encryptedData, iv } = event.detail;

    // 2. 获取code
    const code = await this.pmLogin();

    // 3. 调用登录接口
    wx.request({
      method: "POST",
      url: "http://127.0.0.1:8001/app/user/login/mini",
      data: {
        code,
        encryptedData,
        iv,
      },
      success: ({ data }) => {
        console.log("=== data", data);
        if (data.code !== 1000) {
          wx.showToast({ title: data.message, icon: "none" });
          return;
        }
      },
    });
  },
});
